package com.lottery.ruok.lottrey.adapter;

import android.text.Html;
import android.text.SpannableStringBuilder;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.lottery.ruok.lottrey.R;
import com.lottery.ruok.lottrey.activity.BetListActivity;

import utils.Utils;

/**
 * Created by ASUS on 2017/11/13.
 */

public class BetListAdapter extends BaseAdapter {
    private BetListActivity betListActivity;

    public BetListAdapter(BetListActivity betListActivity) {
        this.betListActivity = betListActivity;
    }

    @Override
    public View getView(final int i, View view, ViewGroup viewGroup) {
        ViewHolder holder;
        if (view != null) {
            holder = (ViewHolder) view.getTag();
        } else {
            view = LayoutInflater.from(betListActivity).inflate(R.layout.bet_list_item, null);
            holder = new ViewHolder();
            holder.descp = view.findViewById(R.id.descp);
            holder.result = view.findViewById(R.id.result);
            holder.number = view.findViewById(R.id.number);
            holder.delete = view.findViewById(R.id.delete);
            holder.total_money = view.findViewById(R.id.total_money);
            holder.profit = view.findViewById(R.id.profit);
            holder.return_manu = view.findViewById(R.id.return_manu);
            view.setTag(holder);
        }

        String number = BetListActivity.results.get(i).show_results;
        SpannableStringBuilder spannableStringBuilder = new SpannableStringBuilder();
        if(number != null) {
            for (int k = 0; k < number.length(); k++) {
                String r = number.charAt(k) + "";
                if (Utils.isNumeric(r)) {
                    switch (r) {
                        case "0":
                            spannableStringBuilder.append(Html.fromHtml("<font color='#c57d7d'>0</font>"));
                            break;
                        case "1":
                            spannableStringBuilder.append(Html.fromHtml("<font color='#9073d5'>1</font>"));
                            break;
                        case "2":
                            spannableStringBuilder.append(Html.fromHtml("<font color='#cb7db4'>2</font>"));
                            break;
                        case "3":
                            spannableStringBuilder.append(Html.fromHtml("<font color='#5884f9'>3</font>"));
                            break;
                        case "4":
                            spannableStringBuilder.append(Html.fromHtml("<font color='#4768d2'>4</font>"));
                            break;
                        case "5":
                            spannableStringBuilder.append(Html.fromHtml("<font color='#3f9ab0'>5</font>"));
                            break;
                        case "6":
                            spannableStringBuilder.append(Html.fromHtml("<font color='#81b272'>6</font>"));
                            break;
                        case "7":
                            spannableStringBuilder.append(Html.fromHtml("<font color='#c8b050'>7</font>"));
                            break;
                        case "8":
                            spannableStringBuilder.append(Html.fromHtml("<font color='#ee9963'>8</font>"));
                            break;
                        case "9":
                            spannableStringBuilder.append(Html.fromHtml("<font color='#eb6e3c'>9</font>"));
                            break;
                    }
                } else {
                    spannableStringBuilder.append(r);
                }
            }
        }

        holder.number.setText(spannableStringBuilder);
        holder.descp.setText(BetListActivity.results.get(i).name);
        holder.result.setText(Html.fromHtml("<font color='#989898'>赔率</font>" + BetListActivity.results.get(i).odds));
        holder.total_money.setText(Html.fromHtml("<font color='#989898'>总计</font>" + Utils.saveTwoFormat(BetListActivity.results.get(i).money) + "元"));
        holder.profit.setText(Html.fromHtml("<font color='#989898'>可赢</font><font color='#f45300'>" + betListActivity.getProfit(BetListActivity.results.get(i)) + "元</font>"));

        holder.return_manu.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //打开注单设定界面
                betListActivity.openOrderSet(BetListActivity.results.get(i), i);
            }
        });

        holder.delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                BetListActivity.results.remove(i);
                betListActivity.recordMoneyList.remove(i);
                notifyDataSetChanged();

                betListActivity.setMoney();
            }
        });

        return view;
    }

    static class ViewHolder {
        TextView descp;
        TextView result;
        TextView number;
        TextView total_money;
        ImageView delete;
        ImageView return_manu;
        TextView profit;
    }

    @Override
    public long getItemId(int i) {
        return 0;
    }

    @Override
    public Object getItem(int i) {
        return null;
    }

    @Override
    public int getCount() {
        return BetListActivity.results.size();
    }
}
